import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



/**
 * @author 0901723m
 *
 */
public class PhoneMonthLayout{

	private PhoneLayout phone;	// incorporates the standard phone interface

	private JPanel cards;

	public JPanel displayBar;

	public JPanel monthView;

	public JPanel buttonsBar;

	public JPanel daysBar;

	public JButton[] buttons;

	public JPanel panel = new JPanel();

	public int currentD, currentM, currentY;

	public Font eventFont = new Font( "Arial", Font.BOLD, 14 );

	private List<Appointment> aps;

	private List<CalendarDate> recur;

	public CalendarDate today;

	public String firstDayOfMonth;

	public HashMap<String, Integer> mapDays; 

	public ButtonActionListener buttonListener;

	public int i = 0, j = 0;

	public PhoneMonthLayout(PhoneLayout phone){
		this.phone = phone;
		cards = phone.middleCards;

		buttonListener = new ButtonActionListener();

		today = phone.date;

		currentD = today.day;
		currentM = today.month; 		// jan is randomly 0..
		currentY = today.year;

		int daysInMonth = phone.monthValues[currentM];

		CalendarDate start_date = new CalendarDate(1,currentM,currentY);
		CalendarDate end_date = new CalendarDate(daysInMonth,currentM,currentY);

		aps = phone.cal.getAppointmentsBetweenDates(start_date, end_date);

		firstDayOfMonth = CalendarDate.getDay(1, currentM, currentY);

		mapDays = new HashMap<String, Integer>(7);			// yeah, i made a hashmap
		mapDays.put("MONDAY", 0);
		mapDays.put("TUESDAY", 1);
		mapDays.put("WEDNESDAY", 2);
		mapDays.put("THURSDAY", 3);
		mapDays.put("FRIDAY", 4);
		mapDays.put("SATURDAY", 5);
		mapDays.put("SUNDAY", 6);


		monthView= new JPanel();
		daysBar= new JPanel();
		buttonsBar= new JPanel();
		displayBar= new JPanel();

		daysBar.setPreferredSize(new Dimension(480,20));
		daysBar.setLayout(new GridLayout(1,7));
		daysBar.setBackground(Color.pink);

		buttonsBar.setPreferredSize(new Dimension(480,400));
		buttonsBar.setLayout(new GridLayout(0,7));
		buttonsBar.setBackground(Color.white);

		displayBar.setPreferredSize(new Dimension(480,210));
		//displayBar.setLayout(new GridLayout(1,7));
		displayBar.setBackground(Color.white);

		monthView.setBackground(Color.white);
		monthView.setPreferredSize(new Dimension(480,640));



		//Font buttonFont = new Font("Arial",Font.PLAIN,20);

		String dayList = "Mon          Tue          Wed          Thu           Fri            Sat           Sun";

		JLabel topString = new JLabel(dayList, JLabel.CENTER);
		daysBar.add(topString);


		buttons = new JButton[45];
		ActionListener listener = new MonthActionListener();
		ActionListener swiper = new SwipeButtonListener();

		phone.swipeLeftBut.addActionListener(swiper);
		phone.swipeRightBut.addActionListener(swiper);

		int startOfGrid = mapDays.get(firstDayOfMonth);
		ArrayList<Integer> redButtons = new ArrayList<Integer>();
		j = 1;
		int totalButtons = startOfGrid + daysInMonth;
		for ( i = 0; i < totalButtons; i++){
			if (i != 0){
				CalendarDate searchStart_date = new CalendarDate(i,currentM,currentY);
				CalendarDate searchEnd_date = new CalendarDate(i,currentM,currentY);
				aps = phone.cal.getAppointmentsBetweenDates(searchStart_date, searchEnd_date);
				if (aps.size() > 0) {
					redButtons.add(i);
				}
			}
			if (i < startOfGrid){
				buttons[i] = new JButton("");
				buttonsBar.add(buttons[i]);
				buttons[i].addActionListener(listener);
				buttons[i].setEnabled(false);
			}else if (j > daysInMonth){
				buttons[i] = new JButton("");
				buttonsBar.add(buttons[i]);
				buttons[i].addActionListener(listener);
				buttons[i].setVisible(true);
			}else{
				buttons[i] = new JButton((j)+"");
				buttonsBar.add(buttons[i]);
				buttons[i].addActionListener(listener);
				j++;
			}
		}
		
		for (int i: redButtons){ // set buttons red that need to be
			buttons[i+(startOfGrid-1)].setForeground(Color.red);
			buttons[i+(startOfGrid-1)].setFont(eventFont);
		}

		monthView.add(daysBar);
		monthView.add(buttonsBar);
		monthView.add(displayBar);
		cards.add(monthView, "Month");
	}

	public void updateTopText(String s) {
		phone.setTopText(s);
	}

	public void updateView() {

		for (JButton b : buttons){
			b = null;
		}

		buttonsBar.removeAll();
		displayBar.removeAll();
		today = phone.date;

		currentD = today.day;
		currentM = today.month; 		// jan is randomly 0..
		currentY = today.year;

		int daysInMonth = phone.monthValues[currentM];

		CalendarDate start_date = new CalendarDate(1,currentM,currentY);
		CalendarDate end_date = new CalendarDate(daysInMonth,currentM,currentY);

		aps = phone.cal.getAppointmentsBetweenDates(start_date, end_date);

		firstDayOfMonth = CalendarDate.getDay(1, currentM, currentY);


		buttons = new JButton[45];
		ActionListener listener = new MonthActionListener();

		ArrayList<Integer> redButtons = new ArrayList<Integer>();
		int startOfGrid = mapDays.get(firstDayOfMonth);
		j = 1;
		int totalButtons = startOfGrid + daysInMonth;
		for ( i = 0; i < totalButtons; i++){
			if (i != 0 && j>=startOfGrid){
				CalendarDate searchStart_date = new CalendarDate(i,currentM,currentY);
				CalendarDate searchEnd_date = new CalendarDate(i,currentM,currentY);
				aps = phone.cal.getAppointmentsBetweenDates(searchStart_date, searchEnd_date);
				if (aps.size() > 0){
					redButtons.add(i);
				}
			}
			if (i < startOfGrid){
				buttons[i] = new JButton("");
				buttonsBar.add(buttons[i]);
				buttons[i].addActionListener(listener);
				buttons[i].setEnabled(false);
			}else if (j > daysInMonth){
				buttons[i] = new JButton("");
				buttonsBar.add(buttons[i]);
				buttons[i].addActionListener(listener);
				buttons[i].setEnabled(false);
			}else{
				buttons[i] = new JButton((j)+"");
				buttonsBar.add(buttons[i]);
				buttons[i].addActionListener(listener);
				j++;
			}

		}
		for (int i: redButtons){ // set buttons red that need to be
			buttons[i+(startOfGrid-1)].setForeground(Color.red);
			buttons[i+(startOfGrid-1)].setFont(eventFont);
		}
		monthView.add(daysBar);
		monthView.add(buttonsBar);
		monthView.add(displayBar);
		monthView.revalidate();
		monthView.repaint();
	}


	class MonthActionListener implements ActionListener{

		public MonthActionListener(){

		}
		public JPanel createAppLabel( Appointment ap ) {
			JPanel panel = new JPanel( new BorderLayout() );
			panel.setPreferredSize(new Dimension(480,25));
			panel.setBackground(Color.white);
			JLabel description = new JLabel( "     " + ap.description );
			JLabel location = new JLabel( "   " + ap.location + "  ");
			location.setFont(new Font("Arial", Font.ITALIC,11));
			JLabel time = new JLabel( "  "+timeStr( ap.start_time ) + " - " + timeStr( ap.end_time ) );
			time.setFont(new Font("Arial", Font.BOLD, 16));
			JButton button = new JButton();
			button.setText(ap.getID()+ "");
			button.setSize(new Dimension(480, 50));
			button.setOpaque(false);
			button.setContentAreaFilled(false);
			button.setBorderPainted(false);
			button.setFont(new Font("arial", Font.ITALIC, 0));
			button.addActionListener(buttonListener);
			panel.add(button);
			panel.add( BorderLayout.WEST, time );
			panel.add( BorderLayout.CENTER, description );
			panel.add( BorderLayout.EAST, location );
			panel.revalidate();
			panel.repaint();

			return ( panel );
		}
		public JPanel createSeperator( int thickness ) {
			JPanel seperator = new JPanel();
			seperator.setPreferredSize(new Dimension(480,thickness));
			seperator.setBackground(Color.darkGray);
			seperator.revalidate();
			seperator.repaint();
			return ( seperator );
		}
		public String timeStr( CalendarTime time ) {
			String timeStr[] = time.toString().split(":");

			if ( timeStr[ 0 ].length() < 2 ) {
				timeStr[ 0 ] = "0" + timeStr[ 0 ];
			}

			if ( timeStr[ 1 ].length() < 2 ) {
				timeStr[ 1 ] += "0";
			}

			return ( timeStr[ 0 ] + ":" + timeStr[ 1 ] );
		}

		public void actionPerformed(ActionEvent ae) {
			panel.removeAll();
			displayBar.removeAll();
			JButton day = (JButton) ae.getSource();
			int dayInt = Integer.parseInt(day.getText());

			CalendarDate searchStart_date = new CalendarDate(dayInt,currentM,currentY);
			CalendarDate searchEnd_date = new CalendarDate(dayInt,currentM,currentY);
			aps = phone.cal.getAppointmentsBetweenDates(searchStart_date, searchEnd_date);
			panel.setPreferredSize(new Dimension(480,210));
			panel.setBackground(Color.white);
			for ( Appointment ap : aps ) {
				panel.add( createAppLabel( ap ) );
				panel.add( createSeperator( 1 ) );
			}
			panel.revalidate();
			panel.repaint();
			displayBar.add(panel);
			displayBar.revalidate();
			displayBar.repaint();
			monthView.add(displayBar);
			monthView.revalidate();
			monthView.repaint();
		}


	}
	class ButtonActionListener implements ActionListener {

		public ButtonActionListener() {

		}

		public void actionPerformed(ActionEvent ae){ 
			CardLayout cl = (CardLayout)(phone.cards.getLayout());
			JButton button = (JButton) ae.getSource();
			phone.editScreen.editEventID(Integer.parseInt(button.getText())); // the text is the id
			cl.show(phone.cards, "EDIT_EVENT");
		}
	}
	class SwipeButtonListener implements ActionListener {

		public void actionPerformed( ActionEvent ae ) {

			CardLayout cl = (CardLayout) phone.middleCards.getLayout();
			if ( ae.getSource() == phone.swipeRightBut ) {
				if (phone.date.month == 12){
					phone.date.month = 1;
					phone.date.day = 1;
					phone.monthView.updateTopText(phone.getDateString(currentM+1));
					phone.monthView.updateView();
					cl.show(phone.middleCards, "Month");
				}else{
					phone.date.day = 1;
					phone.date.month = phone.date.month+1;
					phone.monthView.updateTopText(phone.getDateString(currentM+1));
					phone.monthView.updateView();
					cl.show(phone.middleCards, "Month");
				}
			}
			else if ( ae.getSource() == phone.swipeLeftBut ) {
				if (phone.date.month == 1){
					phone.date.month = 12;
					phone.date.day = 1;
					phone.monthView.updateTopText(phone.getDateString(currentM-1));
					phone.monthView.updateView();
					cl.show(phone.middleCards, "Month");
				}else{
					phone.date.day = 1;
					phone.date.month = phone.date.month-1;
					phone.monthView.updateTopText(phone.getDateString(currentM-1));
					phone.monthView.updateView();
					cl.show(phone.middleCards, "Month");
				}
			}
		}
	}
}






